import { useState, useContext } from "react";
import { Sender } from "@ant-design/x";
import AiChatContext from "@/common/context/aiChatContext.js";
import { themeStore } from "@/common/store/index";

const { getters } = themeStore();

const ChatInput = () => {
  const style = {
    backgroundColor: getters.getInputBgColor()[getters.getTheme()],
  };

  const context = useContext(AiChatContext);

  const [value, setValue] = useState("");

  const handleSubmit = (nextVal) => {
    let sessionId = "";

    if (context.chatX.length) {
      // 从chatX第二个对象拿，因为sse对话第二个才有sessionId，第一个为用户自己发的
      sessionId = context.chatX[1].sessionId;
    }

    context.requestFetch({ text: nextVal, sessionId: sessionId ?? "" });

    setValue("");
    !context.hasMsg && context.setHasMsg(true);
  };

  const inputStyle = {
    input: {
      color: getters.getTextColor()[getters.getTheme()],
    },
  };

  return (
    <Sender
      style={style}
      styles={inputStyle}
      placeholder="你好~ 按 shift+enter 换行"
      // loading={agent.isRequesting()}
      value={value}
      onChange={setValue}
      onSubmit={handleSubmit}
    />
  );
};
export default ChatInput;
